<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="评分标准科目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">
              <!--              <a-input v-model="model.subjectId" placeholder="请输入科目id"  ></a-input>-->
              <a-select v-model="model.subjectId" placeholder="请选择标准所属科目"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in subjectList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评分标准编码" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="achievementStandardCode">
              <a-input v-model="model.achievementStandardCode" placeholder="请输入标准编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评分标准名称" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="achievementStandardName">
              <a-input v-model="model.achievementStandardName" placeholder="请输入标准名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评分标准类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="achievementStandardTtype">
              <j-dict-select-tag type="list" v-model="model.achievementStandardTtype"
                                 dictCode="achievement_standard_ttype" placeholder="请选择评分标准类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="标准用途" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="achievementStandardPurpose">
              <j-dict-select-tag type="list" v-model="model.achievementStandardPurpose"
                                 dictCode="achievement_standard_purpose" placeholder="请选择标准用途" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="用途类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achiStandPurposeType">
              <j-dict-select-tag type="list" v-model="model.achiStandPurposeType"
                                 dictCode="achi_stand_purpose_type" placeholder="请选择标准用途类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="标准序号" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="achievementStandardOrder">
              <a-input-number v-model="model.achievementStandardOrder" placeholder="请输入标准序号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
              <j-dict-select-tag type="list" v-model="model.izOpen"
                                 dictCode="iz_open" placeholder="请选择是否启用" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <j-dict-select-tag v-model="model.sysOrgCode" placeholder="请选择所属部门"
                                 dictCode="sys_depart,depart_name,id" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { getSubjectList } from '@api/achievementCommonAPI'

export default {
  name: 'AchAchievementStandardForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/achachievementstandard/achAchievementStandard/add',
        edit: '/achachievementstandard/achAchievementStandard/edit',
        queryById: '/achachievementstandard/achAchievementStandard/queryById'
      },
      departDataList: [],
      subjectList: []
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.getSubjectData()
  },
  methods: {
    add() {
      //获取当前登录用户信息，并获取部门
      let loginUserData = window.localStorage.getItem('pro__Login_Userinfo')
      loginUserData = JSON.parse(loginUserData)
      this.modelDefault.sysOrgCode = loginUserData.value.orgCode//获取了部门

      this.modelDefault.achievementStandardTtype = 5 //默认本校标
      this.modelDefault.achievementStandardPurpose = '11' //默认用途类型为 试卷
      this.modelDefault.izOpen = 1 //默认启用
      this.modelDefault.delFlag = 0 //默认未删除 正常
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    async getSubjectData() {
      let res = await getSubjectList()
      this.subjectList = []
      res.result.records.forEach(item => {
        this.subjectList.push({
          label: item.subjectName,
          value: item.id
        })
      })
    }

  }
}
</script>